package nl.science.uva.pico.operator;

import nl.science.uva.pico.Stack;
import nl.science.uva.pico.operand.Operand;

/** 
 * Mult multiplies 2 Integer variables. 
 * 
 * @author Ivo van Dongen, Karel Pieterson
 * @version 1.0
 */
public class Mult extends Operator {

	public Mult(Stack stack) {
		super(stack, null, null);
	}

    /** 
     * Multiplies 2 Integers
     * 
     * <p> Mult This instruction calculates x1 * x2 for integer constants x1 and x2 
     * at the top of the stack.
     *
     * @throws IllegalStateException
     *  
     */
	@Override
	public void perform() {
		Integer[] operands = new Integer[2];
		
		//Check number of operands
		if(getStack().size() >= 2) {
			operands[0] = getStack().pop().getIntegerValue();
			operands[1] = getStack().pop().getIntegerValue();
			
			Integer result = operands[0] * operands[1];
			getStack().push(new Operand(result));
			
		} else {
			throw new IllegalStateException(" Too few operands, Mult needs 2 operands");
		}
	}

}
